今天將快速建立一個簡單的Spring入門範例,內容包含如下
在Day01的Overview圖中顯示Container只需要以下模組就可以成功運行Container囉
不過實際在運行Spring時還需要import他底層運行logging所需的JAR,另外我們還需要測試過程中需要的Junit JAR檔
public class User {
private String name;
private String gender;
private String email;
//getter、setter、toString 略
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user01" class="com.swj.User">
<property name="name" value="James"></property>
<property name="gender" value="male"></property>
<property name="email" value="abc@test.com.tw"></property>
</bean>
</beans>
@Test
public void TestIoC(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
User user = (User)ioc.getBean("user01");
System.out.println(user);
}
測試結果